﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace SocialNetworkDAL
{
    public class CRUD
    {
        public static string GenerateMD5Hash(string password)
        {
            MD5 cryptor = new MD5CryptoServiceProvider();
            string cryptingKey = "SuperSecretKey";
            byte[] MD5PasswordArray = cryptor.ComputeHash(Encoding.UTF8.GetBytes(password + cryptingKey));
            StringBuilder MD5Password = new StringBuilder();
            for (int i = 0; i < MD5PasswordArray.Length; i++)
            {
                MD5Password.Append(String.Format("{0:X}", MD5PasswordArray[i]));
            }
            return MD5Password.ToString();
        }

        public static void AddUser(string username, string password)
        {
            SocialNetworkContext dbContext = new SocialNetworkContext();
            User user = new User
            {
                Username = username,
                Password = GenerateMD5Hash(password)
            };
            dbContext.Add(user);
            dbContext.SaveChanges();
        }
        
        static void Main()
        {
            AddUser("SomeUser","SomeUserPass");
        }
    }
}
